package com.droidbank;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;


import com.droidbank.model.Account;
import com.droidbank.model.DataModel;
import com.droidbank.model.Expense;
import com.droidbank.util.FormatDecimal;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Main Expense Overview
 * @author VertigoSol
 *
 */
public class ExpenseActivity extends Activity{

	private Account account;
	private ExpenseAdapter expenseAdapter;
	private double runningTotal;
	
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expense_layout);
        
        Bundle b = getIntent().getExtras();
        account = ((DataModel)getApplication()).getAccounts().get(b.getInt(DataModel.ACCOUNT_INDEX, 0));
        
        runningTotal=0;
        
        final ListView lv = (ListView) findViewById(R.id.expenseList);
        
        //setup the row adapter
        expenseAdapter = new ExpenseAdapter(this, R.layout.expense_row, account.getExpensesAL());
		lv.setAdapter(expenseAdapter);
        
		lv.setSelection(account.getExpenses().size()-1);
		
//		final Dialog editDialog = new Dialog(this);
//        editDialog.setContentView(R.layout.edit_expense_layout);
//        editDialog.setTitle("Edit Login");
//        editDialog.setCancelable(true);
//		
//		
//        Button delButton =(Button) editDialog.findViewById(R.id.removeExpButtonDiag);
//        delButton.setOnClickListener(new OnClickListener() {
//            public void onClick(View v) 
//            {
//            	int i= lv.getSelectedItemPosition();
//            	if(i > 0)
//            	account.removeExpense(account.getExpensesAL().get(i));
//            	expenseAdapter.notifyDataSetChanged();
//            }
//        });
//        
//        
//		lv.setOnItemLongClickListener(new OnItemLongClickListener() {
//
//			public boolean onItemLongClick(AdapterView<?> arg0, View view,
//					int arg2, long arg3) {
//				
//				editDialog.show();
//				
//				return true;
//			}
//		  });
		
		
    }
	
	private class ExpenseAdapter extends ArrayAdapter<Expense> {

        private ArrayList<Expense> items;

        public ExpenseAdapter(Context context, int textViewResourceId, ArrayList<Expense> items) {
                super(context, textViewResourceId, items);
                this.items = items;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.expense_row, null);
                }
                Expense o = items.get(position);
                if (o != null) {
                	
                		//ImageView image = (ImageView) v.findViewById(R.id.expenseImage);
//                		
//                		if(o.getAmount() >=0)
//                			image.setImageResource(R.drawable.aup);
//                		else
//                			image.setImageResource(R.drawable.adown);
                	
                        TextView lt = (TextView) v.findViewById(R.id.toptextleft);
                        TextView lb = (TextView) v.findViewById(R.id.bottomtextleft);
                        
                       // TextView mt = (TextView) v.findViewById(R.id.toptextmid);
                        TextView mb = (TextView) v.findViewById(R.id.bottomtextmid);
                        
                        TextView rt = (TextView) v.findViewById(R.id.toptextright);
                        TextView rb = (TextView) v.findViewById(R.id.bottomtextright);
                        
                        //date and name
                        if (lb != null) {
                              lb.setText(o.getName());                            }
                        if(lt != null){
                        	Date d = new Date(o.getDate());
                        	lt.setText(DateFormat.getDateInstance(DateFormat.MEDIUM).format(d).toString());
                        }
                        
                        //type
                        if(mb!= null)
                        	mb.setText(o.getType());
                        
                        //do temp cache
                        if(o.tempTotal == null){
                        
                        //tally backwards
                        if(position  == items.size()-1)
                        	runningTotal=(account.getAmount() + account.getStartingAmount());
                        
                        //amount/total amount
                        if(rb != null)
                        	rb.setText(String.valueOf(FormatDecimal.format(2,runningTotal)));
                        
                        o.tempTotal = runningTotal;
                        
                        runningTotal-=o.getAmount();
                        
                        }
                        else
                        {
                        	  //amount/total amount
                            if(rb != null)
                            	rb.setText(String.valueOf(FormatDecimal.format(2,o.tempTotal)));
                        }
                        
                        
                        if(rt != null)
                        	rt.setText(String.valueOf(FormatDecimal.format(2,o.getAmount())));
                        
                        
                        double val = o.getAmount();
                    	if(val < 0)
                    		//red
                    		rt.setTextColor(0xFFCD0000);
                    	else
                    		//lime green
                    		rt.setTextColor(0xFF4EEE94);
                        
                }
                return v;
        }
        
	}
	
	
}
